home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Examples / DemoDialogs / UStructureInspectors.cp < prev    next >
Encoding:
Text File  |  1996-04-03  |  16.2 KB  |  527 lines  |  [TEXT/MPS ]

  1. //----------------------------------------------------------------------------------------
  2. // UStructureInspectors.cp
  3. //  Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
  4. //----------------------------------------------------------------------------------------
  5.  
  6. #ifndef __USTRUCTUREINSPECTORS__
  7. #include "UStructureInspectors.h"
  8. #endif
  9.  
  10. // MacApp
  11.  
  12. #ifndef __UAPPLICATION__
  13. #include "UApplication.h"
  14. #endif
  15.  
  16. #ifndef __UMACAPPGLOBALS__
  17. #include "UMacAppGlobals.h"
  18. #endif
  19.  
  20. #ifndef __UMACAPPUTILITIES__
  21. #include "UMacAppUtilities.h"
  22. #endif
  23.  
  24. #ifndef __UWINDOW__
  25. #include "UWindow.h"
  26. #endif
  27.  
  28.  
  29. //========================================================================================
  30. // Global Functions
  31. //========================================================================================
  32. static Boolean ListsMatch(TList* firstList,TList* secondList);
  33. static short GetDepth(TView* aView);
  34. static void FlattenTargetChain(TEventHandler* head, TList* theList);
  35.  
  36. //========================================================================================
  37. // CLASS TIdler
  38. //========================================================================================
  39. #undef Inherited
  40. #define Inherited TEventHandler
  41.  
  42. #pragma segment ARes
  43. MA_DEFINE_CLASS_M1(TIdler, Inherited);
  44.  
  45. //----------------------------------------------------------------------------------------
  46. // TIdler constructor 
  47. //----------------------------------------------------------------------------------------
  48. #pragma segment ARes
  49.  
  50. TIdler::TIdler() 
  51. {
  52.     fHandler = NULL;
  53. } // TIdler::TIdler 
  54.  
  55. //----------------------------------------------------------------------------------------
  56. // TIdler destructor
  57. //----------------------------------------------------------------------------------------
  58. #pragma segment MADestructorRes
  59.  
  60. TIdler::~TIdler()
  61. {
  62. }
  63.  
  64. //----------------------------------------------------------------------------------------
  65. // TIdler::IIdler: 
  66. //----------------------------------------------------------------------------------------
  67. #pragma segment ARes
  68.  
  69. void TIdler::IIdler(TEventHandler* itsHandler)     
  70. {
  71.     this->IEventHandler(NULL);
  72.     
  73.     fHandler = itsHandler;
  74.     this->SetIdleFreq(fHandler->GetIdleFreq());
  75. } // TIdler::IIdler 
  76.  
  77. //----------------------------------------------------------------------------------------
  78. // TIdler::DoIdle: 
  79. //----------------------------------------------------------------------------------------
  80. #pragma segment ARes
  81.  
  82. Boolean TIdler::DoIdle(IdlePhase phase)    // override 
  83. {
  84.     if (fHandler)
  85.         return fHandler->DoIdle(phase);
  86.     else 
  87.         return FALSE;
  88. } // TIdler::DoIdle 
  89.  
  90.  
  91. //========================================================================================
  92. // GLOBAL Procedures
  93. //========================================================================================
  94. #undef Inherited
  95.  
  96. //----------------------------------------------------------------------------------------
  97. // ListsMatch: 
  98. //----------------------------------------------------------------------------------------
  99. Boolean ListsMatch(TList* firstList,TList* secondList)
  100. {
  101.     if (firstList->GetSize() != secondList->GetSize())
  102.         return FALSE;
  103.     else
  104.     {
  105.         //return EqualBlocks(*((Handle) firstList), *((Handle) secondList),GetHandleSize((Handle) firstList));
  106.         for (ArrayIndex i = 1; i <= firstList->GetSize(); i++)
  107.         {
  108.             if (firstList->At(i) != secondList->At(i))
  109.                 return FALSE;
  110.         }
  111.         return TRUE;
  112.     }
  113. } // ListsMatch 
  114.  
  115.  
  116. //========================================================================================
  117. // CLASS TObjectListView
  118. //========================================================================================
  119. #undef Inherited
  120. #define Inherited TTextListView
  121.  
  122. #pragma segment ARes
  123. MA_DEFINE_CLASS_M1(TObjectListView, Inherited);
  124.  
  125. //----------------------------------------------------------------------------------------
  126. // TObjectListView constructor 
  127. //----------------------------------------------------------------------------------------
  128. #pragma segment ARes
  129.  
  130. TObjectListView::TObjectListView() 
  131. {
  132.     fAList = NULL;
  133.     fBList = NULL;
  134.     fDisplayedList = NULL;
  135.     fIdler = NULL;
  136. } // TObjectListView::TObjectListView 
  137.  
  138. //----------------------------------------------------------------------------------------
  139. // TObjectListView::DoPostCreate: 
  140. //----------------------------------------------------------------------------------------
  141. #pragma segment ARes
  142.  
  143. void TObjectListView::DoPostCreate(TDocument* itsDocument)// override 
  144. {
  145.     Inherited::DoPostCreate(itsDocument);
  146.  
  147.     FailInfo fi;
  148.     
  149.     Try(fi)
  150.     {
  151.         fAList = NewList();
  152.         fBList = NewList();
  153.         fDisplayedList = fAList;
  154.         
  155.         TIdler* anIdler = new TIdler;
  156.         anIdler->IIdler(this);
  157.         fIdler = anIdler;
  158.         fi.Success();
  159.     }
  160.     else
  161.     {
  162.         fAList = (TList*) FreeIfObject(fAList);
  163.         fBList = (TList*) FreeIfObject(fBList);
  164.         fIdler = (TIdler*) FreeIfObject(fIdler);
  165.         fDisplayedList = NULL;
  166.         fi.ReSignal();
  167.     }
  168.     
  169. } // TObjectListView::DoPostCreate 
  170.  
  171. //----------------------------------------------------------------------------------------
  172. // TObjectListView::Free: 
  173. //----------------------------------------------------------------------------------------
  174. #pragma segment ARes
  175.  
  176. TObjectListView::~TObjectListView()    // Override 
  177. {
  178.     fAList = (TList*) FreeIfObject(fAList);
  179.     fBList = (TList*) FreeIfObject(fBList);
  180.     fIdler = (TIdler*) FreeIfObject(fIdler);
  181. } // TObjectListView::Free 
  182.  
  183. //----------------------------------------------------------------------------------------
  184. // TObjectListView::BuildList: 
  185. //----------------------------------------------------------------------------------------
  186. #pragma segment ARes
  187.  
  188. void TObjectListView::BuildList(TList* newList) 
  189. {
  190.     newList->DeleteAll();
  191. } // TObjectListView::BuildList 
  192.  
  193. //----------------------------------------------------------------------------------------
  194. // TObjectListView::IsSynchronized: 
  195. //----------------------------------------------------------------------------------------
  196. #pragma segment ARes
  197.  
  198. Boolean TObjectListView::IsSynchronized() 
  199. {
  200.     TList* theNewList;
  201.     TList* theOldList;
  202.     
  203.     if (fDisplayedList == fAList)
  204.     {
  205.         theNewList = fBList;
  206.         theOldList = fAList;
  207.     }
  208.     else
  209.     {
  210.         theNewList = fAList;
  211.         theOldList = fBList;
  212.     }
  213.         
  214.     this->BuildList(theNewList);
  215.     return ListsMatch(theOldList,theNewList);
  216. } // TObjectListView::IsSynchronized 
  217.  
  218. //----------------------------------------------------------------------------------------
  219. // TObjectListView::Synchronize: 
  220. //----------------------------------------------------------------------------------------
  221. #pragma segment ARes
  222.  
  223. void TObjectListView::Synchronize(Boolean /* redraw */) 
  224. {
  225.     TList* theNewList;
  226.     TList* theOldList;
  227.     
  228.     if (fDisplayedList == fAList)
  229.     {
  230.         theNewList = fBList;
  231.         theOldList = fAList;
  232.     }
  233.     else
  234.     {
  235.         theNewList = fAList;
  236.         theOldList = fBList;
  237.     }
  238.  
  239.     this->BuildList(theNewList);
  240.     if (!ListsMatch(theOldList,theNewList))
  241.     {
  242.         TObject* selectedObject = this->GetSelectedObject();
  243.         
  244.         if (theOldList->GetSize() != theNewList->GetSize())
  245.         {
  246.             this->DelItemLast(fNumOfRows);
  247.             this->InsItemLast((short) theNewList->GetSize());
  248.         }
  249.         fDisplayedList = theNewList;
  250.         if (selectedObject)
  251.             this->SelectObject(selectedObject);
  252.         this->ScrollSelectionIntoView(!kRedraw);
  253.         this->ForceRedraw();
  254.     }    
  255. } // TObjectListView::Synchronize 
  256.  
  257. //----------------------------------------------------------------------------------------
  258. // TObjectListView::Draw: 
  259. //----------------------------------------------------------------------------------------
  260. #pragma segment ARes
  261.  
  262. void TObjectListView::Draw(const VRect& area)        // Override 
  263. {
  264.     if (this->IsSynchronized())
  265.         Inherited::Draw(area);
  266.     // Otherwise, let next idle synchronize
  267. } // TObjectListView::Draw 
  268.  
  269. //----------------------------------------------------------------------------------------
  270. // TObjectListView::DoIdle: 
  271. //----------------------------------------------------------------------------------------
  272. #pragma segment ARes
  273.  
  274. Boolean TObjectListView::DoIdle(IdlePhase /*phase*/)    // override 
  275. {
  276.     this->Synchronize(kRedraw);
  277.     return FALSE;
  278. } // TObjectListView::DoIdle 
  279.  
  280. //----------------------------------------------------------------------------------------
  281. // TObjectListView::Open: 
  282. //----------------------------------------------------------------------------------------
  283. #pragma segment ARes
  284.  
  285. void TObjectListView::Open()    // override 
  286. {
  287.     gApplication->InstallCohandler(fIdler,TRUE);
  288.     Inherited::Open();
  289. } // TObjectListView::Open 
  290.  
  291. //----------------------------------------------------------------------------------------
  292. // TObjectListView::Close: 
  293. //----------------------------------------------------------------------------------------
  294. #pragma segment ARes
  295.  
  296. void TObjectListView::Close()    // override 
  297. {
  298.     gApplication->InstallCohandler(fIdler,FALSE);
  299.     Inherited::Close();
  300. } // TObjectListView::Close 
  301.  
  302. //----------------------------------------------------------------------------------------
  303. // TObjectListView::GetSelectedObject: 
  304. //----------------------------------------------------------------------------------------
  305. #pragma segment ARes
  306.  
  307. TObject* TObjectListView::GetSelectedObject()
  308. {
  309.     return this->GetNthObject(this->FirstSelectedItem());
  310. } // TObjectListView::GetSelectedObject 
  311.  
  312. //----------------------------------------------------------------------------------------
  313. // TObjectListView::GetNthObject: 
  314. //----------------------------------------------------------------------------------------
  315. #pragma segment ARes
  316.  
  317. TObject* TObjectListView::GetNthObject(short n)
  318. {
  319.     if (fDisplayedList && (n > 0) && (n <= fDisplayedList->GetSize()))
  320.         return fDisplayedList->At(n);
  321.     else
  322.         return NULL;
  323. } // TObjectListView::GetNthObject 
  324.  
  325. //----------------------------------------------------------------------------------------
  326. // TObjectListView::SelectObject: 
  327. //----------------------------------------------------------------------------------------
  328. #pragma segment ARes
  329.  
  330. void TObjectListView::SelectObject(TObject* anObject)
  331. {
  332.     ArrayIndex index = fDisplayedList->GetIdentityItemNo(anObject);
  333.     this->SelectItem((short) index,FALSE,FALSE,TRUE);
  334. } // TObjectListView::SelectObject 
  335.  
  336.  
  337. //========================================================================================
  338. // GLOBAL Procedures
  339. //========================================================================================
  340. #undef Inherited
  341.  
  342. //----------------------------------------------------------------------------------------
  343. // GetDepth: 
  344. //----------------------------------------------------------------------------------------
  345. #pragma segment ARes
  346.  
  347. short GetDepth(TView* aView)
  348. {
  349.     if (aView->fSuperView == NULL)
  350.         return 0;
  351.     else
  352.         return 1 + GetDepth(aView->fSuperView);
  353. } // GetDepth 
  354.  
  355.  
  356. //========================================================================================
  357. // CLASS TViewHierarchyView
  358. //========================================================================================
  359. #undef Inherited
  360. #define Inherited TObjectListView
  361.  
  362. #pragma segment ARes
  363. MA_DEFINE_CLASS_M1(TViewHierarchyView, Inherited);
  364.  
  365. //----------------------------------------------------------------------------------------
  366. // TViewHierarchyView destructor
  367. //----------------------------------------------------------------------------------------
  368. #pragma segment MADestructorRes
  369.  
  370. TViewHierarchyView::~TViewHierarchyView()
  371. {
  372. }
  373.  
  374. //----------------------------------------------------------------------------------------
  375. // TViewHierarchyView::BuildList: 
  376. //----------------------------------------------------------------------------------------
  377. #pragma segment ARes
  378.  
  379. void TViewHierarchyView::BuildList(TList* newList) // Override
  380. {
  381.     newList->DeleteAll();
  382.     this->FlattenHierarchy(NULL,newList);
  383. } // TViewHierarchyView::BuildList 
  384.  
  385. //----------------------------------------------------------------------------------------
  386. // TViewHierarchyView::FlattenHierarchy: 
  387. //----------------------------------------------------------------------------------------
  388.  
  389. void TViewHierarchyView::FlattenHierarchy(TView* root, TList* theList)
  390. {
  391.     if (root)
  392.     {
  393.         theList->InsertLast(root);
  394.  
  395.         CSubViewIterator iter(root);
  396.             
  397.         for (TView * theSubView = iter.FirstSubView(); iter.More(); theSubView = iter.NextSubView())
  398.         {
  399.             this->FlattenHierarchy(theSubView, theList);
  400.         }        
  401.     }
  402.     else
  403.     {
  404.         CWMgrIterator iter;
  405.     
  406.         for (WindowRef aWinPtr = iter.FirstWMgrWindow(); iter.More(); aWinPtr = iter.NextWMgrWindow())
  407.         {
  408.             TWindow * aWindow = gApplication->WMgrToWindow(aWinPtr);
  409.             if (aWindow && (aWindow != this->GetWindow()))
  410.                 this->FlattenHierarchy(aWindow, theList);
  411.         }
  412.     }
  413. } // TViewHierarchyView::FlattenHierarchy 
  414.  
  415. //----------------------------------------------------------------------------------------
  416. // TViewHierarchyView::GetItemText: 
  417. //----------------------------------------------------------------------------------------
  418. #pragma segment ARes
  419.  
  420. void TViewHierarchyView::GetItemText(short        anItem,
  421.                                          CStr255&    aString)// override 
  422. {    
  423.     if (anItem <= fDisplayedList->GetSize())
  424.     {
  425.         TView* theView = (TView*) this->GetNthObject(anItem);
  426.  
  427.         if (theView)
  428.         {
  429.             CStr255 indentString = "......................................................................................................................";
  430.             indentString.Length() = GetDepth(theView) * 2;
  431.             
  432.             aString = indentString + theView->GetClassName() + " '" + CStr15(theView->fIdentifier) + "'";
  433.         }
  434.         else
  435.             aString = "Error";
  436.     }
  437. } // TViewHierarchyView::GetItemText 
  438.  
  439. //----------------------------------------------------------------------------------------
  440. // TViewHierarchyView::GetSelectedView: 
  441. //----------------------------------------------------------------------------------------
  442. #pragma segment ARes
  443.  
  444. TView* TViewHierarchyView::GetSelectedView()
  445. {
  446.     return (TView*) this->GetSelectedObject();
  447. } // TViewHierarchyView::GetSelectedView 
  448.  
  449.  
  450. //========================================================================================
  451. // GLOBAL Procedures
  452. //========================================================================================
  453. #undef Inherited
  454.  
  455. //----------------------------------------------------------------------------------------
  456. // FlattenTargetChain: 
  457. //----------------------------------------------------------------------------------------
  458. #pragma segment ARes
  459.  
  460. void FlattenTargetChain(TEventHandler* head, TList* theList)
  461. {
  462.     theList->InsertLast(head);
  463.     if (head->fNextHandler)
  464.         FlattenTargetChain(head->fNextHandler,theList);
  465. } // FlattenTargetChain 
  466.  
  467.  
  468. //========================================================================================
  469. // CLASS TTargetChainView
  470. //========================================================================================
  471. #undef Inherited
  472. #define Inherited TObjectListView
  473.  
  474. #pragma segment ARes
  475. MA_DEFINE_CLASS_M1(TTargetChainView, Inherited);
  476.  
  477. //----------------------------------------------------------------------------------------
  478. // TTargetChainView destructor
  479. //----------------------------------------------------------------------------------------
  480. #pragma segment MADestructorRes
  481.  
  482. TTargetChainView::~TTargetChainView()
  483. {
  484. }
  485.  
  486. //----------------------------------------------------------------------------------------
  487. // TTargetChainView::BuildList: 
  488. //----------------------------------------------------------------------------------------
  489. #pragma segment ARes
  490.  
  491. void TTargetChainView::BuildList(TList* newList) // Override
  492. {
  493.     newList->DeleteAll();
  494.     FlattenTargetChain(gApplication->GetTarget(),newList);
  495. } // TTargetChainView::BuildList 
  496.  
  497. //----------------------------------------------------------------------------------------
  498. // TTargetChainView::GetItemText: 
  499. //----------------------------------------------------------------------------------------
  500. #pragma segment ARes
  501.  
  502. void TTargetChainView::GetItemText(short        anItem,
  503.                                          CStr255&    aString)// override 
  504. {    
  505.     if (anItem <= fDisplayedList->GetSize())
  506.     {
  507.         TEventHandler* theHandler = (TEventHandler*) this->GetNthObject(anItem);
  508.  
  509.         aString = theHandler ? CStr255(theHandler->GetClassName()) + " '" + CStr15(theHandler->fIdentifier) + "'" : CStr255("Error");
  510.     }
  511. } // TTargetChainView::GetItemText 
  512.  
  513. //----------------------------------------------------------------------------------------
  514. // TTargetChainView::GetSelectedHandler: 
  515. //----------------------------------------------------------------------------------------
  516. #pragma segment ARes
  517.  
  518. TEventHandler* TTargetChainView::GetSelectedHandler()
  519. {
  520.     return (TEventHandler*) this->GetSelectedObject();
  521. } // TTargetChainView::GetSelectedHandler 
  522.  
  523. //----------------------------------------------------------------------------------------
  524. // End of UStructureInspectors.cp
  525.  
  526. #pragma segment Inline
  527.